package net.lainiao.zhihu.control.admin;

import net.lainiao.zhihu.common.GridResult;
import net.lainiao.zhihu.common.MyPage;
import net.lainiao.zhihu.model.Qutu;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import net.lainiao.zhihu.service.QutuService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2016/1/19.
 */
@Controller
@RequestMapping(value = "/admin/qutu")
public class AdminQutuControl {
    @Resource
    private QutuService QutuService;

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "admin/qutu";
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    public GridResult list(@RequestParam("page") String page, @RequestParam("rows") String rows) {
        int pageindex = Integer.parseInt(page);
        int pagesize = Integer.parseInt(rows);
        if (pageindex == 0) {
            pageindex = 1;
        }
        if (pagesize == 0) {
            pagesize = 20;
        }
        MyPage myPage = new MyPage();
        myPage.setPageSize(pagesize);
        myPage.setCurrentPage(pageindex);
        List<Qutu> list = QutuService.getPage(myPage);
        GridResult gridResult = new GridResult();
        gridResult.setTotal(myPage.getTotal());
        gridResult.setRows(list);
        return gridResult;
    }

    @ResponseBody
    @RequestMapping(value = "/info")
    public Qutu info(@RequestParam("id") String id) {
        return QutuService.info(id);
    }

    @ResponseBody
    @RequestMapping(value = "/dels")
    public String dels(@RequestParam("ids") String ids) {
        if (QutuService.dels(ids) > 0) {
            return "success";
        }
        return "error";
    }


    @ResponseBody
    @RequestMapping(value = "/save")
    public String save(Qutu Qutu, HttpServletRequest request) {
        if (Qutu.getUrl().indexOf("https") > -1) {
            String url = Qutu.getUrl().replace("https", "http");
            Qutu.setUrl(url);
        }
        if (QutuService.save(Qutu) > 0) {
            return "success";
        }
        return "error";
    }
}
